/**
 *  *2010-4-2 上午12:54:52  add 
 */
package com.base.myproject.client.busi.service.gcode;

import java.util.Date;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseStoreEvent;
import com.base.myproject.client.busi.BaseStoreListener;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.print.PrintDialogX;
import com.base.myproject.client.busi.print.Printer;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.tools.Debug;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

/**
 * 浏览总共是发布的商品信息
 * @author Administrator
 *2010-4-2
 * javazgw@gmail.com
 */
public class GcodeView extends BaseJDBCPanel implements BaseStoreListener,Printer,Listener<BaseEvent> {
	BaseStore<BusiObject> store;
	PagingToolBar toolBar =null;
	Button printer;
	public GcodeView()
	{
		init();
	}

	public void init() {
		toolBar = new PagingToolBar(5,true);
		getStore();
		//initwidget();
		store.addBaseStoreListener(BaseStoreEvent.EventType.LOAD_COMPLETE,this);

		printer   = new Button("打印");
		//printer.setStyleName("printer");
		//printer.setIconStyle("printer");
		//printer.setId("printer");
		printer.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.print_16()));
		
		printer.addListener(Events.Select, this);
	}
	public void initToolBarItems()
	{
		getToolbarll().add(toolBar);
		getToolbarll().remove(sava);
		getToolbarll().remove(fresh);
		getToolbarll().add(printer);
		super.initToolBar();
	}
//	private void initwidget() {
//		
//		this.setHeaderVisible(false);
//		this.setTopComponent(toolBar);
//		this.setLayout(new FlowLayout());
//		this.setScrollMode(Scroll.AUTOY);
//	}

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("b2cgcode");
		type.getField("b2cgcode").setType(String.class);
		type.addField("b2cgname");
		type.getField("b2cgname").setType(String.class);
		type.addField("gcode");
		type.getField("gcode").setType(String.class);
		type.addField("modeltype");
		type.getField("modeltype").setType(String.class);
		type.addField("descript");
		type.getField("descript").setType(String.class);
		type.addField("saleprice");
		type.getField("saleprice").setType(Double.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("b2cgcode");
		info.setKeyname(new String[] { "icode" });
		info
				.setSql("select icode,b2cgcode,b2cgname,gcode,modeltype,descript,saleprice from b2cgcode order by icode desc");
		// info.setParent(mainstore);// 关联主表
		// info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		// info.setLinkparent_chile_columns(new String[] { "upicode" });//
		// 关联主表字段
		
		store = new BaseStore<BusiObject>();
		store.setToolBar(toolBar);
		store.setType(type);
		store.setStoreinfo(info);
		store.initcompleted();
		return store;
	}

	final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
	.create(com.base.myproject.client.tools.GreetingService.class);
	@Override
	public void BaseStoreEventHandle(BaseStoreEvent evt) {
	
		this.removeAll();
		for(int i=0;i<evt.getBasestore().getCount();i++)
		{
			 String hh = "<ul class=\"onegcode\">";
			 hh+="<li class=\"onegcodenum\"><span>编&nbsp;&nbsp;号：</span><strong>"+evt.getBasestore().getValue(i, "b2cgcode")+"</strong></li>";
			 hh+="<li class=\"onegcodeprice\"><span>销售价格：</span><strong>"+evt.getBasestore().getValue(i, "saleprice")+"</strong>元</li>";
			 hh+="<li class=\"onegcodedesc\"><span>产品描述：</span><strong>"+evt.getBasestore().getValue(i, "descript")+"</strong></li>";
		//	 hh+="<li class=\"detail-price clearfix\"><span>一 口 价：</span><strong id=\"J_StrPrice\">196.00</strong>元</li>";
			 hh+="</ul>";
			final OneGcode onegcode = new OneGcode(null,hh);
			String icode = evt.getBasestore().getValue(i, "icode").toString();
//		this.add(new Html(icode));
		this.add(onegcode);
		
		greetingService.getDataSet("select imagepath from b2cgcodeimage where upicode = '"+icode+"'", new AsyncCallback<DataSet>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(DataSet result) {
				String[] url = new String[result.getRowCount()];
				for(int i=0;i<result.getRowCount();i++)
				{
					url[i] = result.getValue(i, "imagepath");
				}
				
				onegcode.setUrl(url);
			}
			
		});
		}
		this.layout();
	}

	@Override
	public void Print() {
		String icode =store.getValue("icode").toString();
		PrintDialogX printdialog = new PrintDialogX("1910","&icode="+icode);
		printdialog.show();

		
	}
	
	int sync = 0;
	/* (non-Javadoc)
	 * @see com.extjs.gxt.ui.client.event.Listener#handleEvent(com.extjs.gxt.ui.client.event.BaseEvent)
	 */
	public void handleEvent(BaseEvent be) {
		
		sync++;
		try {
			if (sync == 1) {

				
				Debug.printf(be.getType(), 4);
				if (be.getType() == Events.Select) {
					
					 if (be.getSource() == printer) {
						

					
						Print() ;
					
					}
				}
			}
		}
		
		finally {
		sync--;
		
	}
	}
	

}
